//
//  MYGSurgicalManSelectView.swift
//  MYDoctor
//
//  Created by 王振明 on 2024/4/25.
//

import UIKit

class MYGSurgicalManSelectView: UIView {
    var dataArr:[MYGMyPatientModel] = []
    var selectIndex:Int = -1
    var selectBlock:((MYGPatientModel) -> ())?
    var dismissBlock:(() -> ())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
        getdatasource()
    }
    @objc func backAction(){
        if let c = dismissBlock {
            c()
        }
    }
    @objc func sureAction(){
        if selectBlock != nil && selectIndex > -1{
            let mod = dataArr[selectIndex]
            var result = MYGPatientModel()
            result.id = mod.id
            result.name = mod.patient.name
            selectBlock!(result)
        }
        backAction()
    }
    func getdatasource() {
        self.isHidden = true
        MYGNetwork<MYGSurgicalApi, MYGPageModel<MYGMyPatientModel>>.request(target: .myHuanzhe(params: ["pageSize":"100"])) {[weak self] item in
            self?.isHidden = false
            self?.dataArr.append(contentsOf: item.list)
            self?.tableView.reloadData()
            let actureHeight = max(min((item.list.count * 60) + 80, Int(screenHeight * 0.7)), 200)
            let topmargin = (screenHeight - CGFloat((actureHeight + 50)))/2.0
            self?.whiteBackView.snp.updateConstraints({ make in
                make.top.equalToSuperview().offset(topmargin)
                make.height.equalTo(actureHeight)
            })
            } failed: { _, error in
                MMYGHUD.showToast(text: error)
            }
    }
    
    func initUI(){
        self.backgroundColor = .clear
        addSubview(whiteBackView)
        whiteBackView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(25)
            make.right.equalToSuperview().offset(-25)
            make.top.equalToSuperview().offset(statusBarHeight + 30)
            make.height.equalTo(200)
        }
        whiteBackView.addSubview(topBtn)
        topBtn.snp.makeConstraints { make in
            make.left.top.equalToSuperview().offset(23)
            make.size.equalTo(CGSize(width: 57.5, height: 21))
        }
        whiteBackView.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(50)
            make.bottom.equalToSuperview()
        }
        addSubviews([backBtn,sureBtn])
        backBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview().offset(-screenWidth/4)
            make.top.equalTo(whiteBackView.snp.bottom).offset(16.5)
            make.size.equalTo(CGSize(width: 108, height: 32))
        }
        sureBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview().offset(screenWidth/4)
            make.top.equalTo(whiteBackView.snp.bottom).offset(16.5)
            make.size.equalTo(CGSize(width: 108, height: 32))
        }
        sureBtn.addCAGradientLayerColor(colors: [RGBA(r: 73, g: 117, b: 236, a: 1).cgColor,RGBA(r: 151, g: 179, b: 255, a: 1).cgColor], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 1, y: 0))

    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var topBtn:UIButton = {
       let cont = UIButton()
        cont.backgroundColor = .rgba(85, 126, 237, 1)
        cont.setTitle("病患", for: .normal)
        cont.layerCornerRadius = 3
        cont.setTitleColor(.white, for: .normal)
        cont.titleLabel?.font = .regular(12)
        return cont
    }()
    lazy var backBtn:UIButton = {
       let cont = UIButton()
        cont.backgroundColor = .rgba(255, 255, 255, 1)
        cont.setTitle("返回", for: .normal)
        cont.layerCornerRadius = 16
        cont.setTitleColor(.rgba(116, 116, 116, 1), for: .normal)
        cont.titleLabel?.font = .medium(14)
        cont.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        return cont
    }()
    lazy var sureBtn:UIButton = {
       let cont = UIButton()
        cont.setTitle("确认", for: .normal)
        cont.layerCornerRadius = 16
        cont.setTitleColor(.white, for: .normal)
        cont.titleLabel?.font = .medium(14)
        cont.addTarget(self, action: #selector(sureAction), for: .touchUpInside)

        return cont
    }()
    lazy var whiteBackView:UIView = {
       let cont = UIView()
        cont.backgroundColor = .white
        cont.layerCornerRadius = 10
        return cont
    }()
    lazy var tableView: UITableView = {
        let control = UITableView()
        control.showsVerticalScrollIndicator = false
        control.delegate = self
        control.dataSource = self
        control.rowHeight = UITableView.automaticDimension
        control.separatorStyle = .none
        control.register(cellWithClass: MYGSurgicalManSelectViewTableViewCell.self)
        return control
    }()
}
extension MYGSurgicalManSelectView:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: MYGSurgicalManSelectViewTableViewCell.self, for: indexPath)
        cell.selectionStyle = .none
        let model = dataArr[indexPath.row]
        cell.modelIn = model
        cell.selectBtn.isSelected = selectIndex == indexPath.row ? true : false
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectIndex = indexPath.row
        tableView.reloadData()
        
    }
}
